package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.CustomerHelper;
import com.Helper.FlightHelper;
import com.Helper.PaymentHelper;
import com.Helper.ReservationHelper;
import com.Helper.ReservationInfo;
import com.Helper.SeatHelper;
import com.database.*;

public class ModPaymentServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	
	String resID = null;
	String username = null;
	String flightID = null;
	String seatID = null;
	
	// total extra price
	String extra_price = null;
	
	// paid extra on credit
	String extra_credit = null;
	
	// paid extra on point
	String extra_point = null;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		String msg = null;
		ConnectDB();
								
		username = req.getParameter("username");
		resID = req.getParameter("resID");
		flightID = req.getParameter("flightID");
		seatID = req.getParameter("seatID");
		
		extra_price = req.getParameter("extra_price");
		extra_credit = req.getParameter("extra_credit");
		extra_point = req.getParameter("extra_point"); 
				
		try //if the seat is not taken, user can make a payment
		{
			SeatHelper.Setup(connection);
			PaymentHelper.Setup(connection);
			ReservationHelper.Setup(connection, username);
			
			ReservationInfo oldResInfo = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID));
			
			
			if (NaN(extra_credit) || NaN(extra_credit))//if number is invalid, user will directed to the error page
			{
				msg = "Invalid Number";
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
			}
			else
			{
				float paidPrice = Float.parseFloat(extra_credit);				
				int paidPoint = Integer.parseInt(extra_point);
				float totalPrice = Float.parseFloat(extra_price);
								
				if (paidPrice + paidPoint < totalPrice)
				{
					msg = "Inadequate Amount";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else if (paidPrice + paidPoint > totalPrice)
				{
					msg = "Your paid more than the price. Please go back and adjust your payment";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else if (PaymentHelper.HasEnoughPoints(username, paidPoint) == false)
				{
					msg = "Not enough loyalty points";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else
				{									
					CustomerHelper.AddPoint(username, -paidPoint);
					ReservationHelper.ModPaidPrice(paidPrice + oldResInfo.paidPrice, Integer.parseInt(resID));
					ReservationHelper.ModPaidPoint(paidPoint + oldResInfo.paidPoint, Integer.parseInt(resID));
					
					ReservationHelper.ModSeat(seatID, Integer.parseInt(resID));
					SeatHelper.MarkEmpty(oldResInfo.seat, oldResInfo.flightID);
					SeatHelper.MarkReserved(seatID, flightID);	
					
					int point = CustomerHelper.GetSelectedCustomer(username).point;
					
					msg = "Reservation Modified";
					req.setAttribute("msg", msg);
					req.setAttribute("username", username);
					req.setAttribute("point", point);
					req.getRequestDispatcher("/reservation/ModifiedResult.jsp").forward(req, resp);
				}
			}
		} 
		catch (SQLException e1) 
		{
			msg = "Reservation Failed.";
			e1.printStackTrace();
			req.setAttribute("msg", msg);
			req.setAttribute("username", username);
			req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);	
		}
	}

}

